<?php

namespace App\Http\Controllers\Api\V1;

use App\Admin\Repositories\AreaJobPosition;
use App\Enums\ResponseCode;
use App\Http\Controllers\Api\AbstractApiController;
use App\Http\Resources\JobpositionResource;
use App\Http\Resources\PlazaListResource;
use App\Models\BannerModel;
use App\Models\PlazaModel;
use App\Services\Banner\BannerService;
use App\Services\Jobposition\JobpositionService;
use App\Services\News\NewsService;
use App\Services\Plaza\PlazaService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PlazaController extends AbstractApiController
{

    public function index(Request $request, PlazaService $service): JsonResponse
    {
        $user = auth('api')->user();
        $result = $service->list($user);
        return $this->success(trans("success"), ResponseCode::OK, new PlazaListResource($result));
    }
    /**
     * 轮播图
     * @return JsonResponse
     */
    public function bannerLists(Request $request): JsonResponse
    {
        $platform_id = $request->input('platform_id', '');
        $result = BannerService::list(BannerModel::GENRE_PLAZA);
        $plaza_ids = (new JobpositionService)->get_platform_plaza($platform_id);
        $plaza_list = [];
        foreach ($result as &$plaza) {
            if (in_array($plaza['id'], $plaza_ids)) {
                array_push($plaza_list, $plaza);
            }
        }
        $result = $plaza_list;

        return $this->success(trans("success"), ResponseCode::OK, $result);
    }

    /**
     * 专区列表
     * @param Request $request
     * @param PlazaService $service
     * @return JsonResponse
     */
    public function areaList(Request $request, PlazaService $service): JsonResponse
    {
        $platform_id = $request->input('platform_id', '');
        $input = $request->validate([
            'plaza_id' => 'required|int|min:1',
        ]);
        $user = auth('api')->user();
        $result = $service->areaList($user, $input['plaza_id'], $platform_id);
        return $this->success(trans("success"), ResponseCode::OK, new JobpositionResource($result));
    }
}
